import Mgr from '@/utils/SecurityService'
import store from '@/store'
import { getToken, setToken, removeToken } from '@/utils/auth'

const state = {
  token:getToken(),
  name:'',
  avatar:'',
  userInfo:{}
}
const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
  }
}

const mgr = new Mgr()

const actions = {
  setLoginedUser({commit},user){
    commit('SET_USERINFO', user)
    commit('SET_TOKEN',user.access_token)
    commit('SET_NAME',user.profile.name)
    setToken(user.access_token)
    
    alert(store.getters.name)
  },
  /**
   * 跳转登录
   */
  loginRedirect({commit}){
    return new Promise((resolve,reject) =>{
       mgr.getSignedIn().then(user =>{       
        if(user){
          commit('SET_USERINFO', user)
          commit('SET_TOKEN',user.access_token)
          commit('SET_NAME',user.profile.name)
          setToken(user.access_token)
        }
         resolve(user)
       },err =>{
        reject(err)
       })
    })
  },
  /*
  *登录成功后回调
  */
  signinRedirectCallback({commit}){
    return new Promise((resolve,reject) =>{
      mgr.signinRedirectCallback().then(user =>{
        if(user){
          commit('SET_USERINFO', user)
          commit('SET_TOKEN',user.access_token)
          commit('SET_NAME',user.name)
          setToken(user.access_token)
         }
        resolve(user)
      },err =>{
        reject(err)
      })
    })
  },
  /*
  *登出
  */
  signOut({commit,state}){
    return new Promise((resolve,reject) =>{
      mgr.getSignOut().then(resp =>{
        commit('SET_USERINFO', {})
        commit('SET_TOKEN', '')
        removeToken()
        resolve(resp)
      },err =>{
        reject(err)
      })
    })
  },
  /**
   * 获取token
   */
  getAccessToken({commit,state}){
    return new Promise((resolve,reject) =>{
      mgr.getAcessToken().then(
        acessToken => {
          commit('SET_TOKEN', acessToken)
          setToken(acessToken)
          resolve(acessToken)        
        },
        err => {
          console.log(err)
          reject(err)
      })
    })
 },
 /**
  * 获取用户
  */
 getUser({commit,state}){
  return new Promise((resolve,reject) =>{
    mgr.getUser().then(user =>{
      if(user){
        commit('SET_USERINFO', user)
        commit('SET_TOKEN',user.access_token)
        commit('SET_NAME',user.name)
      }
      resolve(user)
    },err =>{
      reject(err)
    })
  })
 },
signinSilentCallback({commit,state}){
  return new Promise((resolve,reject) =>{
    mgr.signinSilentCallback().then(user =>{
      commit('SET_USERINFO', user)
      commit('SET_TOKEN',user.access_token)
      commit('SET_NAME',user.name)
      setToken(user.access_token)
    },err =>{

    })
  })
 },
renewToken({commit,state}){
  return new Promise((resolve,reject) =>{
    mgr.renewToken().then(
      newToken => {
        alert('renewToken')
        commit('SET_TOKEN', newToken)
        setToken(newToken)
        resolve(newToken)
      },
      err => {
        console.log(err)
        reject(err)
    }) 
  })
}
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
// export default class UserService{
//   /**
//    * 登录
//    */
//   signedIn(){
//      mgr.signIn()
//   }
//   /**
//    * 异步登录
//    */
//   async signedInAsync(){
//     await mgr.getSignedIn().then(res =>{
//       if(res){

//       }else{

//       }
//     },err =>{

//     })
//  }
//   /**
//    * 登出
//    */
//   signOut(){
//      mgr.signOut()
//   }
//   /**
//    * 获取AccessToken
//    */
//   async getAccessToken(){
//     await mgr.getAcessToken().then(
//      acessToken => {
//        console.log(acessToken)
//      },
//      err => {
//        console.log(err)
//    })
//  }

//   async getToken(){
//     await mgr.getUser().then(
//         token => {
//           console.log(token)
//         },
//         err => {
//           console.log(err)
//       })
//     }

//     async getTokenId(){
//         await  mgr.getIdToken().then(
//           tokenId => {
//               console.log(tokenId)
//           },
//           err => {
//             console.log(err)
//         })
//   }
//   async renewToken(){
//      await mgr.renewToken().then(
//       newToken => {
//         console.log(newToken)
//       },
//       err => {
//         console.log(err)
//     }) 
//   }
// }
